apiready = function(){
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper", "../../dialog"], function(V, $api, $, app, service, Swiper, dialog) {
    var timeModel = {
        template: '<i>{{countTime || "00.00.00.0"}}</i>',
        props: {
            time: {
                default: '00.00.00.0'
            },
            countTime: '00.00.00.0'
        },
        mounted: function() {
            var self = this;
            var countTime = function() {
                var count = 0;

                var timer = setInterval(function() {
                    var td = self.time.split('-').join('/');
                    var timeNumber = 9,
                        twoTime = 0;
                    var now = (new Date(td)) - (new Date());
                    var hh = Math.floor(now / (60 * 60 * 1000));
                    var mm = Math.floor(now % (60 * 60 * 1000) / (60 * 1000));
                    var ss = Math.floor(now % (60 * 60 * 1000) % (60 * 1000) / 1000);
                    clearInterval(_timer);
                    if (now <= 0) {
                        clearInterval(timer);
                        return;
                    }
                    var _timer = setInterval(function() {
                        timeNumber--;
                        twoTime++;
                        self.countTime = (hh >= 10 ? hh : "0" + hh) + ":" + (mm >= 10 ? mm : "0" + mm) + ":" + (ss >= 10 ? ss : "0" + ss) + "." + timeNumber;
                        if (timeNumber <= 0) {
                            timeNumber = 9;
                        }
                        if (twoTime == 20) {
                            clearInterval(_timer);
                        }
                    }, 50);
                }, 1000);
            }
            countTime();
        }
    };
    var vm = {
        el: '.container',
        data: {
            goodsId: '',
            shopInfo: {}, // 店铺22
            goodsInfo: {
                tag: null
            }, // 商品信息
            goodsPrice_def: '', // 展示价格
            goodsStock_def: '', // 展示库存
            goodsImages: [], // banner图片列表
            groupList: [], // 拼团列表
            choicList: [], // 选中的拼团列表
            peopleGroup: [], // 拼团人数列表
            commentList: [], // 评论列表
            commentPage: 1, // 评论页数
            hasMoreComment: true, // 是否有更多评论
            isCollect: false, // 是否已收藏
            collectId: null, // 收藏id
            groupPrice: '', // 拼团价格
            goodsOpion: [], // 商品型号列表
            choicOption: [], // 选中的商品型号
            isGroup: false, // 是否是拼团
            isIntoGroup: false, // 是否是加入拼团
            goodsNumber: 1, // 商品数量
            optionNav: 1, // 切换底部列表
            shareShow: false, // 是否显示分享
            tipsInfo: {}, // 商城提示消息
            tipsList: [], // 商城提示列表
            tipsMsg: {}, // 提示内容
            otherUserInfo: {}, // 客服信息
            allStock: 0, // 商品总库存
            allCount: 0, // 总销售量
            iconImg: ['../../../image/shop/active-1.png', '../../../image/shop/active-2.png', '../../../image/shop/active-3.png', '../../../image/shop/active-4.png'],
            selectIcon: '../../../image/shop/active-1.png',
            bgColor: ['rgb(255,245,235)', 'rgb(249,246,253)', 'rgb(255,241,244)', 'rgb(245,250,254)'],
            selectColor: '#fff',
            fontColor: ['rgb(251,123,3)', 'rgb(59,151,241)', 'rgb(234,44,88)', 'rgb(127,16,173)'],
            selectFontColor: '#000',
            startTime: '', // 开始时间
            endTime: '', // 秒杀结束时间
            showTime: '', // 倒计时
            isOver: false, // 秒杀时间是否结束
            isNotStart: false, // 秒杀是否已经开始
            userInfo: '', // 获取用户身份
            isSelectO: false,
            isClickComment: false,
            cartCount: '', // 购物车数量
            tipTime: '', //--提示定时器
            bs: "" //ajax弹出服务器提示控制变量
        },
        components: {
            "time-model": timeModel
        },
        methods: {
            //--服务
            goodsServer: function(et) {
                this.$refs.goodsServeDetail.classList.add("active");
            },
            //--确定服务
            goodsServeDetailBg: function(et) {
                this.$refs.goodsServeDetail.classList.remove("active");
            },
            //--单独购买
            goodsTypeDetail: function(et) {
              // if (!$api.getStorage('Vstatus')) {
              //   app.toast("体验更多功能，请前往登录")
              // }else{
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    this.isGroup = false;
                    this.$refs.goodsTypeDetail.classList.add("active");
                }
              // }
            },
            //--关闭单独购买
            goodsTypeDetailBg: function(et) {
                this.$refs.goodsTypeDetail.classList.remove("active");
            },
            //--发起拼团
            sponsorPartIn: function(isIntoGroup) {
              // if (!$api.getStorage('Vstatus')) {
              //   app.toast("体验更多功能，请前往登录")
              // }else{
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    this.isGroup = true;
                    if (isIntoGroup) {
                        // 参与拼团
                        this.isIntoGroup = true;
                    } else {
                        // 发起拼团
                        this.isIntoGroup = false;
                    }
                    this.$refs.goodsTypeDetail.classList.add("active");
                    this.takepartInClose();
                    this.groupNumberMoreClose();
                }
              // }
            },
            //--查看更多
            groupNumberMore: function(et) {
                this.$refs.groupGoodsList.classList.add("active");
            },
            //--关闭查看更多
            groupNumberMoreClose: function(et) {
                this.$refs.groupGoodsList.classList.remove("active");
            },
            //--参与拼团
            takePartIn: function(et) {
                this.$refs.groupGoodsMy.classList.add("active");
                // this.$refs.groupGoodsList.style.top = '100%';

                this.choicList = this.groupList[et];
                this.peopleGroup = this.groupList[et].already_group_people;
            },

            //--关闭参与拼团
            takepartInClose: function(et) {
                this.$refs.groupGoodsMy.classList.remove("active");
            },
            showGoodsyon: function() {
                this.$refs.goodsyon.classList.add("active");
            },
            goodsyonBg: function() {
                this.$refs.goodsyon.classList.remove("active");
            },

            // 添加我的足迹
            addUserStep: function() {
                var that = this;
                app.ajax(
                    service['addUserStep'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                unionid: $api.getStorage("shop_unionid"),
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code != 0) {
                            if (that.bs != "") {
                                api.toast({
                                    msg: '添加足迹失败',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    }
                );
            },

            // 显示分享
            showShare: function() {
                this.shareShow = true;
            },
            hideShare: function() {
                this.shareShow = false;
            },

            // 获取购物车数量
            getCartCount: function() {
                var that = this;
                app.ajax(
                    service['cartGetCartCount'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.cartCount = res.data;
                            if (that.cartCount > 0) {
                                that.cartCount > 10 ? $('.js-cart-count').text('10+') : $('.js-cart-count').text(that.cartCount);
                                $('.js-cart-count').show();
                            } else {
                                $('.js-cart-count').hide();
                            }

                        } else {
                            // api.toast({
                            //     msg: res.message,
                            //     duration: 2000,
                            //     location: 'bottom'
                            // });
                        }
                    }
                );
            },

            // 获取商城提示消息
            getGoodsInformation: function(time_stamp) {
                var that = this;
                app.ajax(
                    service['getGoodsInformation'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                time_stamp: time_stamp,
                                goods_id: that.goodsId,
                                mask: 'none',
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.tipsInfo = res.data;
                            that.tipsList = res.data.list;
                            that.showTips();
                        } else {
                            // api.toast({
                            //     msg: res.message,
                            //     duration: 2000,
                            //     location: 'bottom'
                            // });
                        }
                    }
                );
            },

            // 显示提示
            showTips: function() {
                var that = this;
                if (this.tipsList.length > 1) {
                    this.tipsMsg = {
                        avater: that.tipsList[0].avater,
                        name: that.tipsList[0].nickname,
                        msg: that.tipsList[0].msg,
                        goodsId: that.tipsList[0].goods_id,
                        groupId: that.tipsList[0].group_id
                    };
                    this.tipsList = this.tipsList.splice(0, 1);
                    $('.loop-tips').fadeIn('fast', function() {
                        $(this).css('display', 'flex');
                    });
                    that.tipTime = setTimeout(function() {
                        $('.loop-tips').fadeOut();
                        that.showTips();
                    }, 3000);
                } else {
                    that.tipTime = setTimeout(function() {
                        that.getGoodsInformation(that.tipsInfo.time_stamp);
                    }, 5000);
                }
            },

            // 分享到微信
            shareToWx: function(type) {
                var that = this;
								if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                }else{
									var wx = api.require('wx');
	                var toWhere;
	                if (type == 1) {
	                    // 分享给朋友
	                    toWhere = 'session';
	                } else if (type == 2) {
	                    // 分享到朋友圈
	                    toWhere = 'timeline';
	                }

	                app.downloadImg(that.goodsInfo.cover, function(img_url) {
	                    that.getShareLink(function(url) {
	                        wx.shareWebpage({
	                            scene: toWhere,
	                            title: that.goodsInfo.name,
	                            description: that.choicOption.name,
	                            contentUrl: url,
	                            thumb: img_url
	                        }, function(ret, err) {
	                            if (ret.status) {
	                                api.toast({
	                                    msg: '分享成功',
	                                    duration: 2000,
	                                    location: 'bottom'
	                                });
	                                api.toast({
	                                    msg: '分享失败',
	                                    duration: 2000,
	                                    location: 'bottom'
	                                });
	                            }
	                        });
	                    });
	                });
								}
            },

            // 获取分享连接
            getShareLink: function(callback) {
                var that = this;
                app.ajax(
                    service['Share'], {
                        data: {
                            values: {
                                type: 2,
                                unionid: $api.getStorage("shop_unionid"),
                                share_id: that.goodsId
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            var url = res.data;
                            callback(url);
                        } else {
                            api.toast({
                                msg: '获取分享链接失败',
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 查看大图
            showBigImg: function(index) {
                var that = this;
                var photoBrowser = api.require('photoBrowser');
                var urlArr = [];
                for (var i = 0; i < that.goodsImages.length; i++) {
                    urlArr.push(that.goodsImages[i].url);
                }

                photoBrowser.open({
                    images: urlArr,
                    bgColor: '#000',
                    activeIndex: index
                }, function(ret, err) {
                    if (ret.eventType == 'click') {
                        photoBrowser.close();
                    }
                });
            },

            //--确定参与拼团
            takepartInSure: function() {
                var listInfo = this.choicList;

                var optionId = $('.get-data').find('.active').attr('data-optionId'); // 规格
                var quantity = $('.number-goods').val(); // 数量

                if (!optionId) {
                    api.toast({
                        msg: '请选择商品型号',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                var str;
                if (!this.isIntoGroup) {
                    // 发起拼团
                    str = optionId + ',' + quantity + ',1,0';
                } else {
                    // 参与拼团
                    var customer_id = listInfo.group_id;
                    str = optionId + ',' + quantity + ',1,' + customer_id;
                }
                clearTimeout(this.tipTime);
                app.openWin('buyOrder', {
                    options: str
                }); // 生成订单
                this.$refs.goodsTypeDetail.classList.remove("active");
            },

            // 立即购买
            buyNow: function() {
                var that = this;
                var optionId = $('.get-data').find('.active').attr('data-optionId'); // 规格
                var quantity = $('.number-goods').val(); // 数量
                if (!optionId) {
                    api.toast({
                        msg: '请选择商品型号',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (quantity > this.choicOption.stock) {
                    api.toast({
                        msg: '库存不足',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }

                var str = optionId + ',' + quantity + ',0,0';
                clearTimeout(that.tipTime);
                app.openWin('buyOrder', {
                    options: str
                }); // 生成订单
                this.$refs.goodsTypeDetail.classList.remove("active");
                that.choicInfo = null;
            },

            //--颜色，尺寸，型号的选择
            goodsTypeModel: function(et) {
                var ul = et.target.parentNode.children;
                for (var i = 0; i < ul.length; i++) {
                    ul[i].classList.remove("active");
                }
                et.target.classList.add("active");

                var index = et.target.dataset.index;
                this.choicOption = this.goodsOpion[index];
                this.goodsNumber = 1;
                this.isSelectO = true;
            },

            //--加
            plusMinusPlus: function(et) {
                if (this.choicOption) {
                    if (this.goodsNumber < this.choicOption.stock) {
                        this.goodsNumber += 1;
                    }
                }
            },
            //--减
            plusMinusPlusMinus: function(et) {
                if (this.goodsNumber >= 2) {
                    this.goodsNumber -= 1;
                }
            },

            //--数量输入
            plusMinusInput: function(et) {

            },

            //--颜色与尺码的选择
            selectColor: function(et) {

            },
            selectSize: function(et) {

            },

            init: function() {
                var that = this;
                var goodsId = api.pageParam.goodsId;

                this.goodsId = goodsId;

                // 获取商品信息
                this.getGoodsInfo(function() {

                    // 秒杀倒计时
                    if (that.goodsInfo.tag == 1) {
                        that.showTime = {
                            h: '00',
                            m: '00',
                            s: '00'
                        }
                        that.countTime();
                        that.$nextTick(function() {
                            $('.js-show').fadeIn('slow');
                        });
                        var startTime = that.startTime.split('-').join('/');
                        if ((new Date(startTime)) > (new Date())) {
                            that.isNotStart = true;
                        } else {
                            var endTime = that.endTime.split('-').join('/');
                            if ((new Date(endTime)) <= (new Date())) {
                                that.isOver = true;
                                that.selectIcon = '../../../image/shop/buy/buy-13.png';
                                that.selectColor = '#fff';
                                that.selectFontColor = '#000';
                            }
                        }
                    } else {
                        that.$nextTick(function() {
                            $api.setTimeout(function() {
                                $('.js-show').fadeIn('slow');
                            }, 100)
                        })
                    }

                    // 添加我的足迹
                    that.addUserStep();

                    // 购物车数量
                    that.getCartCount();

                    // 监听商城消息
                    that.getGoodsInformation(0);

                    // 监听输入事件
                    $('.number-goods').on('input', function() {
                        var inputVal = this.value;
                        if (inputVal >= that.choicOption.stock) {
                            this.value = that.choicOption.stock;
                        }
                    });

                    // 初始化轮播
                    $api.setTimeout(function() {
                        var mySwiper = new Swiper('.swiper-container', {
                            pagination: {
                                el: '.swiper-pagination',
                                type: 'fraction'
                            },
                        })
                    }, 900);
                });
            },

            // 获取拼团商品详细信息
            getGoodsInfo: function(callback) {
                var that = this;

                app.ajax(
                    service['getGoodsDetail'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                customer_unionid: $api.getStorage("shop_unionid")
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.goodsInfo = res.data;
                            that.shopInfo = res.data.shop; // 店铺信息
                            that.goodsImages = res.data.goods_images; // banner图片列表
                            that.goodsPrice_def = res.data.goods_options[0].sale_price; // 展示价格
                            that.goodsStock_def = res.data.goods_options[0].stock; // 展示库存
                            that.groupList = res.data.group_list; // 拼团列表
                            that.groupPrice = res.data.goods_options[0].group_price; // 拼团列表
                            that.goodsOpion = res.data.goods_options; // 商品型号
                            that.choicOption = res.data.goods_options[0]; // 默认选中第一个
                            that.otherUserInfo = res.data.exchange; // 店主信息
                            that.allStock = res.data.shop.all_stock; // 商品总库存
                            that.allCount = res.data.shop.all_count; // 已出售总数
                            that.endTime = res.data.date_end; // 结束时间
                            that.startTime = res.data.date_start; // 秒杀开始时间

                            // 检测是否收藏
                            if (!res.data.collect_id) {
                                that.isCollect = false;
                            } else {
                                that.isCollect = true;
                                that.collectId = res.data.collect_id;
                            }

                            if (res.data.tag == 1 && !that.isOver) {
                                // 秒杀
                                that.selectIcon = that.iconImg[0];
                                that.selectColor = that.bgColor[0];
                                that.selectFontColor = that.fontColor[0];
                            } else if (res.data.tag == 2) {
                                // 清仓
                                that.selectIcon = that.iconImg[3];
                                that.selectColor = that.bgColor[3];
                                that.selectFontColor = that.fontColor[3];
                            } else if (res.data.tag == 3) {
                                // 折扣
                                that.selectIcon = that.iconImg[2];
                                that.selectColor = that.bgColor[2];
                                that.selectFontColor = that.fontColor[2];
                            } else if (res.data.tag == 4) {
                                // 9.9
                                that.selectIcon = that.iconImg[1];
                                that.selectColor = that.bgColor[1];
                                that.selectFontColor = that.fontColor[1];
                            } else {
                                that.selectIcon = '../../../image/shop/buy/buy-13.png';
                                that.selectColor = '#fff';
                                that.selectFontColor = '#000';
                            }

                            callback();
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 秒杀倒计时
            countTime: function() {
                var that = this;
                var timer = setInterval(function() {
                    var startTime = that.startTime.split('-').join('/');
                    if ((new Date(startTime)) > (new Date())) {
                        // 秒杀未开始
                        that.isNotStart = true;
                    } else {
                        // 开始秒杀
                        var endTime = that.endTime.split('-').join('/');
                        var now = (new Date(endTime)) - (new Date());
                        var hh = Math.floor(now / (60 * 60 * 1000));
                        var mm = Math.floor(now % (60 * 60 * 1000) / (60 * 1000));
                        var ss = Math.floor(now % (60 * 60 * 1000) % (60 * 1000) / 1000);

                        if ((new Date(endTime)) <= (new Date())) {
                            that.showTime = {
                                h: '00',
                                m: '00',
                                s: '00'
                            }
                            that.isOver = true;
                            that.selectIcon = '../../../image/shop/buy/buy-13.png';
                            that.selectColor = '#fff';
                            that.selectFontColor = '#000';
                            clearInterval(timer);
                        } else {
                            that.showTime = {
                                h: (hh >= 10 ? hh.toString() : "0" + hh),
                                m: (mm >= 10 ? mm.toString() : "0" + mm),
                                s: (ss >= 10 ? ss.toString() : "0" + ss)
                            }
                        }
                    }
                }, 1000);
            },

            // 获取商品评论
            getGoodsComment: function() {
                var that = this;
                var page = this.commentPage;
                app.ajax(
                    service['getGoodsComment'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                shop_code: that.shopInfo.shop_code,
                                page: page
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            if (this.commentPage == 1) {
                                that.commentList = res.data;
                            } else {
                                if (res.data.length > 0) {
                                    for (var i = 0, len = res.data.length; i < len; i++) {
                                        that.commentList.push(res.data[i]);
                                    }
                                    that.hasMoreComment = true;
                                } else {
                                    that.hasMoreComment = false;
                                }
                            }
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 收藏、取消收藏
            favorGoods: function() {
                var that = this;
                var isCollect = that.isCollect;

                var url, arg;

                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    if (isCollect) {
                        // 取消收藏
                        url = service['cancelCollectShop'];
                        arg = {
                            unionid: $api.getStorage("shop_unionid"),
                            favorite_id: that.collectId
                        }
                    } else {
                        // 添加收藏
                        url = service['addToFavoriteShop'];
                        arg = {
                            unionid: $api.getStorage("shop_unionid"),
                            type: 1,
                            goods_id: that.goodsId
                        }
                    }

                    app.ajax(
                        url, {
                            data: {
                                values: arg
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                if (!isCollect) {
                                    if (res.data) {
                                        that.collectId = res.data;
                                    }
                                }
                                that.isCollect = !that.isCollect;
                            } else {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    );
                }

            },

            // 加入购物车
            addTocar: function() {
                var that = this;
                var goodsId = $('.get-data').find('.active').attr('data-goodsId');
                var optionId = $('.get-data').find('.active').attr('data-optionId');
                if (!goodsId && !optionId) {
                    api.toast({
                        msg: '请选择商品型号',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                app.ajax(
                    service['addToCart'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                quantity: $('.number-goods').val(),
                                goods_id: goodsId,
                                option_id: optionId
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            api.toast({
                                msg: '添加到购物车成功',
                                duration: 2000,
                                location: 'middle'
                            });
                            that.getCartCount();
                            api.sendEvent({
                                name: 'lisentCart'
                            });
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 切换
            swtichNavBottom: function(option) {
                this.commentPage = 1;
                if (option == 1) {
                    this.optionNav = 1;
                } else {
                    this.optionNav = 2;
                    // 获取品论列表
                    if (!this.isClickComment) {
                        this.isClickComment = true;
                        this.getGoodsComment();
                    }
                }
            },

            // 触底
            scrolltobottom: function() {
                if (this.optionNav == 2) {
                    if (this.hasMoreComment) {
                        this.commentPage += 1;
                        this.getGoodsComment();
                    }
                }
            },

            // 联系客服
            toChar: function() {
                var that = this;
                var obj = {
                    openid: that.otherUserInfo.openid, //id
                    nickname: that.shopInfo.shop_name, //昵称
                    src: that.shopInfo.shop_logo, //头像
                    new_nickname: that.otherUserInfo.new_nickname, //对象
                }
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    app.yz_chat(obj);
                }

            },

            // 前往店铺首页
            toShop: function() {
                var shopData = this.shopInfo;
                var otherUserId = this.shopInfo.out_user_id;
                app.openWin('buyMain', {
                    shopData: shopData,
                    otherUserInfoId: otherUserId
                });
            },

            // 进店逛逛
            toOtherGoods: function(id, group_booking) {
                var url = '';
                if (group_booking) {
                    url = './group_goods.html';
                } else {
                    url = '../buy_goods/goods.html';
                }
                api.openFrame({
                    name: 'otherGoods',
                    url: url,
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        h: 'auto'
                    },
                    pageParam: {
                        goodsId: id
                    },
                    reload: true
                });
            },

            // 开启秒杀提醒
            clockMe: function(e) {
                var type = e.target.innerText;
                var that = this;

                var clockType;
                if (type.indexOf('提醒我') != -1) {
                    clockType = 1
                    if (type.indexOf('明天') != -1) {
                        clockType = 2;
                    }
                } else if (type.indexOf('已开启') != -1) {
                    return;
                }

                app.ajax(
                    service['indexGoodsRemind'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                goods_id: that.choicOption.goods_id,
                                remind_type: clockType
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            api.toast({
                                msg: '已开启秒杀提醒',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.goodsInfo.is_remind = 1;
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },
            //--评论查看大图
            lookBigImg: function(et) {
                var ul = et.currentTarget.parentNode.querySelectorAll("img");
                var index = +et.currentTarget.dataset.index;
                var imgSrc = [];
                for (var i = 0; i < ul.length; i++) {
                    imgSrc.push(ul[i].src.replace(/\?x-oss-process=image\/resize,m_lfit,w_960/g, ''));
                }
                api.openWin({
                    name: 'lookBigImg',
                    url: 'widget://src/Shop/buy_person/big_img.html',
                    reload: true,
                    animation: {
                        type: "movein",
                        subType: "from_right",
                        duration: 200
                    },
                    pageParam: {
                        src: imgSrc,
                        idnex: index
                    }
                });
            },
            showBigImgs: function(url) {
                var photoBrowser = api.require('photoBrowser');
                var urlArr = [url];
                photoBrowser.open({
                    images: urlArr,
                    bgColor: '#000',
                    activeIndex: 0
                }, function(ret, err) {
                    if (ret.eventType == 'click') {
                        photoBrowser.close();
                    }
                });
            },
        },

        mounted: function() {
            var self = this;
            // vue执行完毕后
            //--z支付返回刷新
            api.addEventListener({
                name: 'payBackGoods_'
            }, function(ret, val) {
                self.init();
            });

            function btnKeyBack() {
                clearTimeout(self.tipTime);
                $api.setTimeout(function() {
                    api.closeWin();
                }, 200);
            }
            api.addEventListener({
                name: 'keyback'
            }, function() {
                btnKeyBack();
            });

            //--点击上面按钮返回
            $api.addEvt($api.dom('#btn-back'), 'click', function() {
                btnKeyBack();
            });

            //--右滑
            api.addEventListener({
                name: 'swiperight'
            }, function(ret, err) {
                btnKeyBack();
            });

        },
        computed: {
            // 获取所在地省级地址
            locationChange: function() {
                return this.shopInfo.shop_location.split(' ')[1];
            },
            // 时间判断
            timeChange: function() {
                var that = this;
                var start = this.goodsInfo.date_start.split(' ');
                var now = that.startTime.split('-').join('/');

                var td = new Date();
                td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
                var od = new Date(now);
                od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
                var xc = (od - td) / 1000 / 60 / 60 / 24;
                if (xc < 0) {
                    return "已结束";
                } else if (xc == 0) {
                    return start[1] + '提醒我';
                } else if (xc < 2) {
                    return "明天" + start[1] + '提醒我';
                } else {
                    return "未开始";
                }
            },

            // 获取最低价格
            getLowPrice: function() {
                var option = this.goodsOpion;
                if (option.length < 2) {
                    return this.choicOption.group_price;
                } else {
                    var price = option[0].group_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (parseFloat(option[i].group_price) < parseFloat(price)) {
                            price = option[i].group_price;
                        }
                    }
                    return price;
                }
            },

            // 获取最低价格
            getLowKillPrice: function() {
                var option = this.goodsOpion;
                if (option.length < 2) {
                    return this.choicOption.kill_price;
                } else {
                    var price = option[0].kill_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (parseInt(price) > parseInt(option[i].kill_price)) {
                            price = option[i].kill_price;
                        }
                    }
                    return price;
                }
            },
        },
        created: function() {
            var that = this;
            if ($api.getStorage("user")) {
                this.userInfo = $api.getStorage("user").data;
                this.bs = 1;
            }
            this.init();
            api.addEventListener({
                name: 'scrolltobottom',
                extra: {
                    threshold: 0
                }
            }, function(ret, err) {
                that.scrolltobottom();
            });

            api.addEventListener({
                name: 'toCart'
            }, function(ret, err) {
                if (ret) {
                    app.openWin('buyCart', {
                        channel: 'notIndex'
                    });
                }
            });

            // 监听购物车数量改变
            api.addEventListener({
                name: 'lisentCart'
            }, function(ret, err) {
                if (ret) {
                    that.getCartCount();
                }
            });

            api.addEventListener({
                name: 'shopDetailDoShomeThing'
            }, function(ret, err) {
                if (ret) {
                    var type = ret.value.type;
                    if (type == 0) {
                        // 商城首页
												api.openWin({
														name: 'index',
														// customRefreshHeader: "UIPullRefreshMotive",
														url: 'widget://src/Shop/shop_index/shop_index.html',
														pageParam: {
																channel: 'YK'
														}
												})
                    }
										// else if (type == 1) {
                    //     // 我的名片
                    //     api.closeToWin({
                    //         name: 'root',
                    //         animation: {
                    //             type: 'fade',
                    //             subType: 'from_right',
                    //             duration: 300
                    //         }
                    //     });
                    // }
                }
            });

						$('.buy-head-cart').on('click', function() {
                if (self.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    api.sendEvent({
                        name: 'toCart'
                    });
                }
            });
        }
    };
    var even = function() {
        $('.buy-head-nav').on('click', function() {
            $('.nav-dio-top').toggle();
        });

        $('.nav-dio-top div').on('click', function() {
            var type = $(this).index();
            api.sendEvent({
                name: 'shopDetailDoShomeThing',
                extra: {
                    type: type,
                }
            });
        });
    }
  	var newVue = new Vue(vm);
  	even();
  });
  }
